package com.qianfeng.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qianfeng.dto.Page;
import com.qianfeng.entity.ChargeProject;
import com.qianfeng.entity.ChargeProjectRegistration;
import com.qianfeng.entity.Registration;
import com.qianfeng.mapper.ChargeProjectMapper;
import com.qianfeng.mapper.ChargeProjectRegistrationMapper;
import com.qianfeng.mapper.RegistrationMapper;
import com.qianfeng.service.ChargeProjectRegistrationService;
import com.qianfeng.service.InHospitalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional
public class ChargeProjectRegistrationServiceImpl implements ChargeProjectRegistrationService {
    @Autowired
    private ChargeProjectMapper chargeProjectMapper;
    @Autowired
    private RegistrationMapper registrationMapper;
    @Autowired
    private ChargeProjectRegistrationMapper chargeProjectRegistrationMapper;
    @Autowired
    private InHospitalService inHospitalService;
    @Override
    public void addChargeProjectRegistration(Integer rId,Integer cpId) {
        ChargeProjectRegistration chargeProjectRegistration = new ChargeProjectRegistration();
        chargeProjectRegistration.setCpId(cpId);
        chargeProjectRegistration.setrId(rId);
        chargeProjectRegistration.setCprTime(new Date());
        chargeProjectRegistrationMapper.addChargeProjectRegistration(chargeProjectRegistration);
    }

    @Override
    public Page getChargeProjectRegistrationPage(Integer pageNo,Integer pageSize) {
        int pageNo_=1;
        int pageSize_=2;
        if (pageNo!=null){
            pageNo_=pageNo;
        }
        if (pageSize!=null){
            pageSize_= pageSize;
        }
        PageHelper.startPage(pageNo_,pageSize_);
        List<ChargeProjectRegistration> chargeProjectRegistrations = chargeProjectRegistrationMapper.getChargeProjectRegistrations();

        PageInfo<ChargeProjectRegistration> pageInfo = new PageInfo(chargeProjectRegistrations);
        Page page = new Page();
        page.setPageCount(pageInfo.getPages());
        page.setHasNext(pageInfo.isHasNextPage());


        List<ChargeProjectRegistration> list = pageInfo.getList();
        for (ChargeProjectRegistration chargeProjectRegistration : list) {
            //收费项
            ChargeProject project = chargeProjectMapper.getChargeProjectById(chargeProjectRegistration.getCpId());
            chargeProjectRegistration.setChargeProject(project);
            //病人
            Registration registration = registrationMapper.getRegistrationById(chargeProjectRegistration.getrId());
            chargeProjectRegistration.setRegistration(registration);
        }

        page.setObj(list);

        return page;
    }

    @Override
    public List<ChargeProjectRegistration> getChargeProjectRegistrationList(Integer rId) {
        List<ChargeProjectRegistration> chargeProjectRegistrationList = chargeProjectRegistrationMapper.getChargeProjectRegistrationList(rId);
        for (ChargeProjectRegistration chargeProjectRegistration : chargeProjectRegistrationList) {
            //收费项
            ChargeProject project = chargeProjectMapper.getChargeProjectById(chargeProjectRegistration.getCpId());
            chargeProjectRegistration.setChargeProject(project);
            //病人
            Registration registration = registrationMapper.getRegistrationById(chargeProjectRegistration.getrId());
            chargeProjectRegistration.setRegistration(registration);
        }
        return chargeProjectRegistrationList;
    }
}
